﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tender.Tool
{
    public partial class Form1 : UIForm
    {
        public Form1()
        {
            InitializeComponent();
            uiTreeView1.Dock = DockStyle.Fill;
            string osVersion = GetOSVersion();

            if (osVersion == "Win7")
            {
                Console.WriteLine("当前电脑运行的是 Windows 7");
            }
            else if (osVersion == "Win10")
            {
                Console.WriteLine("当前电脑运行的是 Windows 10");
            }
            else
            {
                Console.WriteLine("当前电脑的操作系统版本未知喵~");
            }
        }
        SqlServer.DbContext dbContext = new SqlServer.DbContext();
        Mysql.DbContextmysql db = new Mysql.DbContextmysql();
        private void uiTreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {         
            if (e.Node.Name =="1") return;
            if(e.Node.Name == "3")
            {
                string str = System.Environment.CurrentDirectory;
                List<XmlList> newObj;
                if (!File.Exists($"{str}\\XmlListdata.xml")) return;
                DataContractSerializer serializer = new DataContractSerializer(typeof(List<XmlList>));
                using (Stream stream = File.OpenRead($"{str}\\XmlListdata.xml"))
                {
                    newObj = (List<XmlList>)serializer.ReadObject(stream);
                }
                splitContainer1.Panel2.Controls.Clear();
                UIDataGridView uIDataGrid = new UIDataGridView();
                uIDataGrid.DataSource = newObj;
                uIDataGrid.Dock = DockStyle.Fill;
                uIDataGrid.SelectionMode= DataGridViewSelectionMode.FullRowSelect;
                uIDataGrid.CellClick += DataGridView_CellClick;
                splitContainer1.Panel2.Controls.Add(uIDataGrid);
            };
            if (e.Node.Name == "1-0")
            {
                View.UserControl2 userco = new View.UserControl2();
                if (userco.InvokeRequired)
                {
                    userco.Invoke(new Action(() =>
                    {
                        splitContainer1.Panel2.Controls.Clear();
                        userco.Dock = DockStyle.Fill;
                        userco.FiowActionclick = fiowActionclick;
                        splitContainer1.Panel2.Controls.Add(userco);
                    }));
                }
                else
                {
                    splitContainer1.Panel2.Controls.Clear();
                    userco.Dock = DockStyle.Fill;
                    userco.FiowActionclick = fiowActionclick;
                    splitContainer1.Panel2.Controls.Add(userco);
                }

            }
            else if (e.Node.Name == "1-2")
            {

                View.UserControl1_2 userco = new View.UserControl1_2();
                if (userco.InvokeRequired)
                {
                    userco.Invoke(new Action(() =>
                    {
                        splitContainer1.Panel2.Controls.Clear();
                        userco.Dock = DockStyle.Fill;
                        userco.Action_Click = action_click;
                        splitContainer1.Panel2.Controls.Add(userco);
                    }));
                }
                else
                {
                    splitContainer1.Panel2.Controls.Clear();
                    userco.Dock = DockStyle.Fill;
                    userco.Action_Click = action_click;
                    splitContainer1.Panel2.Controls.Add(userco);
                }

            }
            else if (e.Node.Name == "1-1-0")
            {
       
                View.UserControl1 userco = new View.UserControl1();
                if (userco.InvokeRequired)
                {
                    userco.Invoke(new Action(() =>
                    {
                        splitContainer1.Panel2.Controls.Clear();
                        userco.Dock = DockStyle.Fill;
                        userco.Actionclick = actionclick;
                        userco.screenshot = screenshotjt;
                        splitContainer1.Panel2.Controls.Add(userco);
                    }));
                }
                else
                {
                    splitContainer1.Panel2.Controls.Clear();
                    userco.Dock = DockStyle.Fill;
                    userco.Actionclick = actionclick;
                    userco.screenshot = screenshotjt;
                    splitContainer1.Panel2.Controls.Add(userco);
                }
             
            }
            else if(e.Node.Name =="2")
            {
                
                View.SqlUserControl usercontrol = new View.SqlUserControl();
                if (usercontrol.InvokeRequired)
                {
                    usercontrol.Invoke(new Action(() =>
                    {
                        splitContainer1.Panel2.Controls.Clear();
                        usercontrol.Dock = DockStyle.Fill;
                        usercontrol.action = selectsql;
                        splitContainer1.Panel2.Controls.Add(usercontrol);
                    }));
                }
                else
                {
                    splitContainer1.Panel2.Controls.Clear();
                    usercontrol.Dock = DockStyle.Fill;
                    usercontrol.action = selectsql;
                    splitContainer1.Panel2.Controls.Add(usercontrol);
                }
               
            }
            else
            {
                View.UserControl3 usercontrol = new View.UserControl3();
                if (usercontrol.InvokeRequired)
                {
                    usercontrol.Invoke(new Action(() =>
                    {
                        splitContainer1.Panel2.Controls.Clear();
                        usercontrol.Dock = DockStyle.Fill;
                        //usercontrol.action = selectsql;
                        splitContainer1.Panel2.Controls.Add(usercontrol);
                    }));
                }
                else
                {
                    splitContainer1.Panel2.Controls.Clear();
                    usercontrol.Dock = DockStyle.Fill;
                    //usercontrol.action = selectsql;
                    splitContainer1.Panel2.Controls.Add(usercontrol);
                }
            }

        }

        private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                var selectedRow = (sender as DataGridView).Rows[e.RowIndex].Cells;
            }
        }


        /// <summary>
        /// 公开招标报名推送2.0
        /// </summary>
        /// <param name="arg1"></param>
        /// <param name="arg2"></param>
        private void action_click(string arg1, string arg2)
        {
            dbContext.SqlConfigstr = $"Data Source=192.168.100.6;Initial Catalog=SRBG_CBM;User ID=sa;Password=asdw@122";
           var data=   dbContext.Where($"SELECT CBM_ContractMaterial.ApplyID from CBM_ContractMaterial WHERE BillNo='{arg1}' AND State<>-1").AsEnumerable().ToList();
            dbContext.SqlConfigstr = $"Data Source=192.168.100.6;Initial Catalog=SRBG_Tender;User ID=sa;Password=asdw@122";
            var data1 = dbContext.Where($"SELECT Ted_ZhaoBiaoBaoMinAudit.BillId  FROM [dbo].[Ted_ZhaoBiaoBaoMinAudit] WHERE ApplyId='{data[0]["ApplyID"]}'AND CompanyName='{arg2}'").AsEnumerable().ToList();
            if (data.Any())
            {
                CPMS.Tender.Service.TenderBasic tender = new CPMS.Tender.Service.TenderBasic();
                tender.Test(data1[0]["BillId"].ToString());
                UIMessageBox.ShowInfo("处理成功", true, true);
            }
            else
            {
                if(data.Count()>1) UIMessageBox.ShowError($"处理失败,不满足处理条件请手动处理", true, true);
                UIMessageBox.ShowError($"处理失败,没有找到数据", true, true);
            }
        }

        /// <summary>
        /// 截图
        /// </summary>
        private void screenshotjt()
        {
            WindowState = FormWindowState.Minimized;
        }

        /// <summary>
        /// 流程问题处理 没有审核历史
        /// </summary>
        /// <param name="obj"></param>
        private void fiowActionclick(string obj, int type, string table, string Database)
        {
            if (string.IsNullOrEmpty(obj)|| string.IsNullOrEmpty(table)|| string.IsNullOrEmpty(Database))
            {
                UIMessageBox.ShowError("参数不能为空", true, true);
                return;
            }
            if (type == 0)
            {
                Noreviewprocess(obj, table, Database);
                return;
            }
            if (type == 1)
            {
                Noreviewprocess1(obj, table, Database);
                return;
            }
            if(type == 2)
            {
                Noreviewprocess2(obj, table, Database);
                return;
            }
            if (type == 3)
            {
                Noreviewprocess3(obj, table, Database);
                return;
            }
        }
        /// <summary>
        /// 审核流程已经完成状态还是审核中
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="table"></param>
        /// <param name="database"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Noreviewprocess3(string obj, string table, string database)
        {
            dbContext.SqlConfigstr = $"Data Source=192.168.100.6;Initial Catalog={database};User ID=sa;Password=asdw@122";
            var data = dbContext.Where($"SELECT * FROM [dbo].[{table}] WHERE BillNo='{obj}' AND State=1").AsEnumerable().ToList();
            if (data.Count()==1)
            {
                CPMS.Tender.Service.TenderBasic tender = new CPMS.Tender.Service.TenderBasic();
                var type=    xmlLists();
                var str= type.Single(t=>t.Value== table);
                try
                {
                    tender.TestAudit((CPMS.Tender.Model.Common.CommonEnum.BillType)str.Tender, data[0]["BillId"].ToString(), false);
                    UIMessageBox.ShowInfo("处理成功", true, true);
                    return;
                }
                catch(Exception ex)
                {
                    throw;
                }           
            }
            else
            {
                string str = data.Count() == 0 ? "没有查询到数据" : "查询到多条数据不满足处理条件";
                UIMessageBox.ShowError($"处理失败,原因:{str}", true, true);
                return;
            }
        }

        /// <summary>
        /// 没有审核历史处理
        /// </summary>
        private void Noreviewprocess(string obj,string table,string Database)
        {
            try
            {
                dbContext.SqlConfigstr = $"Data Source=192.168.100.6;Initial Catalog={Database};User ID=sa;Password=asdw@122";
                var data = dbContext.Where($"SELECT * FROM [dbo].[{table}] WHERE BillNo='{obj}'");
                if (data.Rows.Count > 1)
                {
                    UIMessageBox.ShowError($"处理失败不满足处理条件请手动处理", true, true);
                    return;
                }
                dbContext.SqlConfigstr = "Data Source=192.168.100.150;Initial Catalog=SRBG_FlowEngin;User ID=sa;Password=asdw@123";//切换为流程引擎数据库
                var data1 = dbContext.Where($"SELECT * FROM [WORK]  WHERE WORKID='{data.Rows[0]["WorkId"]}'");
                if (data1 != null && data1.Rows.Count == 0)
                {
                    dbContext.SqlConfigstr = $"Data Source=192.168.100.6;Initial Catalog={Database};User ID=sa;Password=asdw@122";
                    var data2 = dbContext.Update($"UPDATE {table} set WorkId=NULL,State =0  WHERE BillId='{data.Rows[0]["BillId"]}'");
                    if (data2 > 0)
                    {
                        UIMessageBox.ShowInfo("处理成功", true, true);
                        return;
                    }
                    UIMessageBox.ShowError("处理失败", true, true);
                    return;
                }
                else
                {
                    if (data1.Rows[0]["STATE"].ToString() == "2")
                    {
                        var data3 = dbContext.Update($"update WORK set STATE = 0 where WORKID = '{data.Rows[0]["WorkId"]}'");
                        if (data3 > 0)
                        {
                            var data4 = dbContext.Where($"SELECT A.DISPID, B.TASKTYPE, C.UNITINDEX, C.UNITNAME, A.USERNAME, B.WORKID, A.STATE,B.STATE AS TASKSTATE,A.NOTIFYTIME, A.FINISHTIME, B.TASKID, E.PRIORTASKID, A.ISPASS " +
                                $"FROM USERDISP A WITH(NOLOCK) INNER JOIN TASK B WITH(NOLOCK) ON A.TASKID = B.TASKID INNER JOIN UNIT C WITH(NOLOCK) ON B.UNITID = C.UNITID INNER JOIN WORK D WITH(NOLOCK) ON B.WORKID = D.WORKID LEFT " +
                                $"JOIN TASKORDER E ON B.TASKID = E.NEXTTASKID WHERE D.STATE <> 2 AND " +
                                $"D.WORKID = '{data.Rows[0]["WorkId"]}' ORDER BY E.PRIORTASKID");
                            if (data4.Rows.Count > 0)
                            {
                                UIMessageBox.ShowInfo("处理成功", true, true);
                                return;
                            }
                        }
                    }
                    else
                    {
                        var data4 = dbContext.Where($"SELECT A.DISPID, B.TASKTYPE, C.UNITINDEX, C.UNITNAME, A.USERNAME, B.WORKID, A.STATE,B.STATE AS TASKSTATE,A.NOTIFYTIME, A.FINISHTIME, B.TASKID, E.PRIORTASKID, A.ISPASS " +
                                            $"FROM USERDISP A WITH(NOLOCK) INNER JOIN TASK B WITH(NOLOCK) ON A.TASKID = B.TASKID INNER JOIN UNIT C WITH(NOLOCK) ON B.UNITID = C.UNITID INNER JOIN WORK D WITH(NOLOCK) ON B.WORKID = D.WORKID LEFT " +
                                            $"JOIN TASKORDER E ON B.TASKID = E.NEXTTASKID WHERE D.STATE <> 2 AND " +
                                            $"D.WORKID = '{data.Rows[0]["WorkId"]}' ORDER BY E.PRIORTASKID");
                        if (data4.Rows.Count > 0)
                        {
                            UIMessageBox.ShowInfo("处理成功", true, true);
                            return;
                        }
                    }

                }
         
            }
            catch (Exception ex)
            {
                UIMessageBox.ShowError($"处理失败具体原因：{ex.Message}", true, true);
            }
        }
        /// <summary>
        /// 当前单据不是审核中状态
        /// </summary>
        private void Noreviewprocess1(string obj, string table,string Database)
        {
            try
            {
                dbContext.SqlConfigstr = $"Data Source=192.168.100.6;Initial Catalog={Database};User ID=sa;Password=asdw@122";
                var data = dbContext.Where($"SELECT * FROM [dbo].[{table}] WHERE BillNo='{obj}' AND State=2");
                if (data.Rows.Count > 1)
                {
                    UIMessageBox.ShowError($"处理失败不满足处理条件请手动处理", true, true);
                    return;
                }
                dbContext.SqlConfigstr = "Data Source=192.168.100.150;Initial Catalog=SRBG_FlowEngin;User ID=sa;Password=asdw@123";//切换为流程引擎数据库
                var data1 = dbContext.Where($"SELECT * FROM [WORK]  WHERE WORKID='{data.Rows[0]["WorkId"]}'");
                if (data1 != null && data1.Rows.Count>0&& data.Rows[0]["State"].ToString()=="0")
                {

                    dbContext.SqlConfigstr = $"Data Source=192.168.100.6;Initial Catalog={Database};User ID=sa;Password=asdw@122";
                    var dataout = dbContext.Update($"UPDATE {table} SET State=1 WHERE BillNo='{obj}'");
                }
                else
                {
                    UIMessageBox.ShowError($"处理失败没有找到任务", true, true);
                    return;
                }
            }
            catch (Exception ex)
            {
                UIMessageBox.ShowError($"处理失败具体原因：{ex.Message}", true, true);
            }
        }
        /// <summary>
        /// 审核报错
        /// </summary>
        private void Noreviewprocess2(string obj, string table,string Database)
        {

            try
            {
                dbContext.SqlConfigstr = $"Data Source=192.168.100.6;Initial Catalog={Database};User ID=sa;Password=asdw@122";
                var data = dbContext.Where($"SELECT * FROM [dbo].[{table}] WHERE BillNo='{obj}'");//查询单据
                if (data.Rows.Count > 1)
                {
                    UIMessageBox.ShowError($"处理失败不满足处理条件请手动处理", true, true);
                    return;
                }
                if (data.Rows[0]["State"].ToString() == "0"&& !string.IsNullOrEmpty(data.Rows[0]["State"].ToString()))
                {
                    var data2 = dbContext.Update($"UPDATE {table} set WorkId=NULL,State =0  WHERE BillId='{data.Rows[0]["BillId"]}'");
                   if(data2>0) UIMessageBox.ShowInfo("处理成功", true, true);
                    return;
                }//处理状态不是审核中的问题

                dbContext.SqlConfigstr = "Data Source=192.168.100.150;Initial Catalog=SRBG_FlowEngin;User ID=sa;Password=asdw@123";//切换为流程引擎数据库
                var data1 = dbContext.Where($"SELECT * FROM [WORK]  WHERE WORKID='{data.Rows[0]["WorkId"]}'");
                if (data1 != null && data1.Rows.Count > 0)
                {
                    var data4 = dbContext.Where($"SELECT A.DISPID, B.TASKTYPE, C.UNITINDEX, C.UNITNAME, A.USERNAME, B.WORKID, A.STATE,B.STATE AS TASKSTATE,A.NOTIFYTIME, A.FINISHTIME, B.TASKID, E.PRIORTASKID, A.ISPASS " +
                        $"FROM USERDISP A WITH(NOLOCK) INNER JOIN TASK B WITH(NOLOCK) ON A.TASKID = B.TASKID INNER JOIN UNIT C WITH(NOLOCK) ON B.UNITID = C.UNITID INNER JOIN WORK D WITH(NOLOCK) ON B.WORKID = D.WORKID LEFT " +
                        $"JOIN TASKORDER E ON B.TASKID = E.NEXTTASKID WHERE D.STATE <> 2 AND " +
                        $"D.WORKID = '{data.Rows[0]["WorkId"]}' ORDER BY E.PRIORTASKID");
                    if (data.Rows[0]["data"].ToString() == "2")
                    {
                      var d1item=  data4.AsEnumerable().Where(t=>t.Field<int>("STATE") ==1&&t.Field<int>("TASKSTATE") == 1).ToList();
                        if (d1item.Count()==1)
                        {
                            dbContext.Update($"update WORK set STATE = 1 where WORKID = '{data.Rows[0]["WorkId"]}'");
                            dbContext.Update($"update USERDISP set STATE = 2,FINISHTIME = GETDATE(),ISPASS = 1  where DISPID = '{d1item[0]["DISPID"]}'");
                            dbContext.Update($"update TASK set STATE = 2,FINISHTIME = GETDATE()  where TASKID = '{d1item[0]["TASKID"]}'");
                            UIMessageBox.ShowInfo("处理成功", true, true);
                            return;
                        }
                        else
                        {
                            UIMessageBox.ShowWarning("处理失败,不满足处理条件请手动处理", true, true);
                            return;
                        }
                    }
                    
                    if (data4.AsEnumerable().Any())
                    {
                        var itemdata=data4.AsEnumerable().ToList();
                        int indexi = 0;//记录第一个节点
                        string taskid = "";
                   
                        for (int i = 0; i < itemdata.Count; i++)
                        {
                            if (itemdata[i]["TASKSTATE"].ToString() == "1")
                            {
                                if(indexi==0)indexi = i;
                                for (int j = 0; j < itemdata.Count-indexi; j++)
                                {
                                    if(itemdata[i+j]["TASKSTATE"].ToString() == "1"&& itemdata[i + j]["TASKID"].ToString()!= itemdata[i + j-1]["TASKID"].ToString())
                                    {
                                        taskid = itemdata[indexi]["TASKID"].ToString();
                                        goto outerLoop;
                                    }
                                }
                            }
                        }
                    outerLoop:
                        if (!string.IsNullOrEmpty(taskid))
                        {
                           bool yici = true;
                           var data2=  itemdata.Where(x => x["TASKID"].ToString()== taskid);
                            foreach (var item in data2)
                            {
                                if (item["TASKSTATE"].ToString()=="1"|| item["STATE"].ToString() == "1")
                                {
                                    int inde = 0;
                                    if (item["STATE"].ToString() == "1")
                                    {
                                        inde = dbContext.Update($"update USERDISP set STATE = 2,FINISHTIME = GETDATE(),ISPASS = 1  where DISPID = '{item["DISPID"]}'");
                                        if (inde>0&&yici&& item["TASKSTATE"].ToString() == "1")
                                        {
                                            dbContext.Update($"update TASK set STATE = 2,FINISHTIME = GETDATE()  where TASKID = '{item["TASKID"]}'");
                                            yici = false;
                                        }
                                    }
                                    else
                                    {
                                        if (yici)
                                        {
                                            dbContext.Update($"update TASK set STATE = 2,FINISHTIME = GETDATE()  where TASKID = '{item["TASKID"]}'");
                                            yici = false;
                                        }
                                    }
                         
                                }                       
                            }
                            UIMessageBox.ShowInfo("处理成功", true, true);
                        }
                        else
                        {
                            UIMessageBox.ShowWarning("处理失败,不满足处理条件请手动处理", true, true);
                        }
                    }
           

                }
                else
                {
                    UIMessageBox.ShowError($"处理失败没有找到任务", true, true);

                }
            }
            catch (Exception ex)
            {
                UIMessageBox.ShowError($"处理失败具体原因：{ex.Message}", true, true);
            }
        }
        /// <summary>
        /// 执行修复单据企业CompanyId不允许为空方法
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void actionclick(string obj)
        {
            string texno = "";
            if (!sqlchecking(obj)) return;
            string pattern = "^[A-Z]+$";
            string exception = null;
            bool isMatch = Regex.IsMatch(obj, pattern);          
            if (!isMatch)
            {
                dbContext.SqlConfigstr = "TrustServerCertificate=true;Server=192.168.100.6;Database=SRBG_Tender;User Id=sa;Password=asdw@122";
                DataTable dat = dbContext.Where($"SELECT * FROM Ted_ZhaoBiaoBaoMinAudit WHERE  ApplyNo='{obj}' AND CompanyId='' AND CompanyId is not NULL");
              if(dat.Rows.Count==0) UIMessageBox.ShowError("没有查询到数据",true,true);
               StringBuilder sql = new StringBuilder();
                StringBuilder sql1 = new StringBuilder();
                sql.Append("UPDATE Ted_ZhaoBiaoBaoMinAudit SET CompanyId = CASE  BillId");
                int index = 0;
                foreach (var item in dat.AsEnumerable())
                {
                    try
                    {
                        var dataitem1 = db.Where(item["SocialCode"].ToString()).AsEnumerable();
                        var dataitem=  dataitem1.Single(x => x.Field<string>("credit_code") == item["SocialCode"].ToString());
                        if (dataitem["state"].ToString() == "2")
                        {
                            sql.Append($"  WHEN '{item["Billid"]}' THEN '{dataitem["id"]}' ");
                            sql1.Append($"'{item["Billid"]}',");
                            index++;
                        }
                        else
                        {
                            exception += $"外包方还存在审核中的单据:{item["Billid"]} --";
                        }
             
                    }
                    catch (Exception ex)
                    {
                      
                        exception+= item["Billid"].ToString()+"在外包方没有查询到对于供应商或者分包商 --";
                    }
      
                }
                if(sql1.Length>0)
                sql1.Remove(sql1.Length - 1, 1);
                int index1=  dbContext.Update($"{sql}  END WHERE Billid IN ({sql1})");
                if (!string.IsNullOrEmpty(exception))
                {
                    UIMessageBox.ShowSuccess($"修复成功{index1}条数据,Billid为:" + exception, true, true);
                    return;
                }
                UIMessageBox.ShowSuccess($"修复成功{index1}条数据", true, true);
                return;
            }
            ///////////////////////////////////////
            dbContext.SqlConfigstr ="TrustServerCertificate=true;Server=192.168.100.6;Database=SRBG_Tender;User Id=sa;Password=asdw@122";
            DataTable data=  dbContext.Where($"SELECT * FROM Ted_ZhaoBiaoBaoMinAudit WHERE  Billid='{obj}'",out texno);
            if (!string.IsNullOrEmpty(texno))
            {
                UIMessageBox.ShowError("执行失败具体原因："+texno, true, true);
                return;
            }
            if (!string.IsNullOrEmpty(data.Rows[0]["CompanyId"].ToString()))
            {
                UIMessageBox.ShowSuccess("执行成功", true, true);
                return;
            }
            var datarows=  data.Rows[0]["SocialCode"].ToString();
            var mydatarows = db.Where($"SELECT * FROM outer_supplier WHERE credit_code='{datarows}'", out texno);
            if (!string.IsNullOrEmpty(texno))
            {
                UIMessageBox.ShowError("执行失败具体原因：" + texno, true, true);
                return;
            }
            if (mydatarows.Rows.Count == 0)
            {
                var d = db.Where($"SELECT * FROM `outer_subcontractor` WHERE credit_code='{datarows}'");
                if (d.Rows.Count == 0)
                {
                    UIMessageBox.ShowInfo("在外包方库里面没有查询到数据，请确认是否有写入外包方库", true, true);
                    return;
                }
                if (d.Rows[0]["state"].ToString() == "2")
                {
                   
                    dbContext.Update($"UPDATE Ted_ZhaoBiaoBaoMinAudit SET CompanyId='{d.Rows[0]["id"]}'  WHERE Billid='{obj}'");
                }
                else
                {
                    UIMessageBox.ShowInfo("该分包商状态还是在审核中", true, true);
                    return;
                }
            }
            else
            {
                if (mydatarows.Rows[0]["state"].ToString() == "2")
                {
                    dbContext.Update($"UPDATE Ted_ZhaoBiaoBaoMinAudit SET CompanyId='{mydatarows.Rows[0]["id"]}'  WHERE Billid='{obj}'");
                }
                else
                {
                    UIMessageBox.ShowInfo("该供应商状态还是在审核中", true, true);
                    return;
                }
            }
            UIMessageBox.ShowSuccess("执行成功",true,true);
        }
        /// <summary>
        /// SQL查询方法
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private object selectsql(string obj)
        {
            if (string.IsNullOrEmpty(obj))
            {
                UIMessageBox.ShowInfo("请输入SQL语句", true, true);
                return null;
            }
           
            
            SqlServer.DbContext dbContext = new SqlServer.DbContext();
            dbContext.SqlConfigstr = "Data Source=192.168.100.150;Initial Catalog=SRBG_FlowEngin;User ID=sa;Password=asdw@123";
            try
            {
                //ConfigurationManager.AppSettings.Set("T-SQL", obj);
                return dbContext.Where(obj);

            }
            catch (Exception ex)
            {

            }
            return null;
        }

        private void uiTreeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //SqlServer.DbContext dbContext = new SqlServer.DbContext();
            //dbContext.SqlConfigstr = "Data Source=192.168.100.6;Initial Catalog=SRBG_Tender;User ID=sa;Password=asdw@122";
            //try
            //{
            //    var data = dbContext.Where<Models.TedTender>("SELECT TOP(10000) * FROM [dbo].[Ted_Tender]");

            //   uiDataGridView1.DataSource = data;
            //}
            //catch (Exception ex)
            //{
                
            //}
       
        }

        private void uiDataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }
        public bool sqlchecking(string sql)
        {
            if (string.IsNullOrEmpty(sql))
            {
                UIMessageBox.ShowError("输入不允许为空");
                return false;
            }
            return true;
        }

        public static string GetOSVersion()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem");
            ManagementObjectCollection collection = searcher.Get();

            foreach (ManagementObject obj in collection)
            {
                string caption = obj["Caption"] as string;

                if (caption.Contains("Windows 7"))
                {
                    return "Win7";
                }
                else if (caption.Contains("Windows 10"))
                {
                    return "Win10";
                }
            }

            return "Unknown";
        }
        /// <summary>
        /// 读取Xml实体
        /// </summary>
        /// <returns></returns>
        private List<XmlList> xmlLists()
        {
            string str = System.Environment.CurrentDirectory;
            if (!File.Exists($"{str}\\XmlListdata.xml")) return new List<XmlList>();
            DataContractSerializer serializer = new DataContractSerializer(typeof(List<XmlList>));
            using (Stream stream = File.OpenRead($"{str}\\XmlListdata.xml"))
            {
                List<XmlList> newObj = (List<XmlList>)serializer.ReadObject(stream);
                return newObj;
            }
        }
    }
}
